import { parse } from 'querystring'
import pathRegexp from 'path-to-regexp'
/* eslint no-useless-escape:0 import/prefer-default-export:0 */

const reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/
export const isUrl = path => reg.test(path)
export const isAntDesignPro = () => {
  if (ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site') {
    return true
  }

  return window.location.hostname === 'preview.pro.ant.design'
} // 给官方演示站点用，用于关闭真实开发环境不需要使用的特性

export function getNoUndefinedString(obj) {
  return obj || ''
}

export const isAntDesignProOrDev = () => {
  const { NODE_ENV } = process.env

  if (NODE_ENV === 'development') {
    return true
  }

  return isAntDesignPro()
}
export const getPageQuery = () => parse(window.location.href.split('?')[1])
/**
 * props.route.routes
 * @param router [{}]
 * @param pathname string
 */

export const getAuthorityFromRouter = (router = [], pathname) => {
  const authority = router.find(({ path }) => path && pathRegexp(path).exec(pathname))
  if (authority) return authority
  return undefined
}

// 源码里这个方法也没用上
export function getValue(obj) {
  return Object.keys(obj)
    .map(key => obj[key])
    .join(',')
}

/**
 * upload值转换为已上传的内容
 * @param {*} item
 * @param {*} index 序号
 * @param {*} itemType 1为文件 2为图片
 */
export function uploadDataConvert(item, index, itemType) {
  const res = {
    success: false,
    data: {},
  }
  if (item.response && item.response.success) {
    const resData = item.response.data
    const path = JSON.stringify(resData)
    // const path =
    //   itemType === '2'
    //     ? [resData.barName, resData.sourceName, resData.zipName].join(',')
    //     : resData.sourceName
    res.success = true
    res.data = {
      itemType,
      orders: index,
      pathName: item.name,
      path,
    }
  }
  return res
}

/**
 * 已上传的内容转换为upload初始值
 * @param {*} itemType 1为文件 2为图片
 * @param {*} items
 */
export function itemConvertToUpload(itemType, items) {
  return items
    .map(item => {
      if (item.itemType === itemType) {
        const path = JSON.parse(item.path)
        const res = {
          // 为了提供给上传组件回显
          uid: item.id, // 这是上传组件规定的文件唯一标识，内部会提供给Form以便正常渲染回显列表
          name: item.pathName,
          status: 'done',
          // TODO: 此处url加上线上前缀
          url: `${getImgPrefix()}${path.sourceName}`,
          // 为了迎合最终向后端提交附件时方便新老文件统一处理
          response: {
            success: true,
            data: path,
          },
        }
        // if (itemType === '1') {
        //   res.response.data = { barName: '', sourceName: item.path, zipName: '' }
        // } else {
        //   const [barName, sourceName, zipName] = item.path.split(',')
        //   res.response.data = { barName, sourceName, zipName }
        // }
        return res
      }
      return null
    })
    .filter(item => item || false)
}

/**
 * 图片域名
 */
export function getImgPrefix() {
  // return process.env.NODE_ENV === 'production' ? '/' : 'http://snp-us.top'
  return ''
}
